package com.gxa.scdx.cloud.user.service.impl;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.gxa.scdx.cloud.user.pojo.PyData;
import com.gxa.scdx.cloud.user.pojo.Task;
import com.gxa.scdx.cloud.user.pojo.TypeConst;
import com.gxa.scdx.cloud.user.service.TaskService;
import org.json.JSONException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * 解析并处理 数据
 *
 * @author 颜可翔
 */
@Service
public class DataServiceImpl {

    @Autowired
    private TaskService taskService;

    // 任务数据处理
    public  void fileProcess (String data) throws JsonProcessingException {
        String replacedData = data.replaceAll("__", "\\\\\\\\");

        PyData pyData = getPyData(replacedData);

        String taskData = pyData.getData();

        // 将单引号替换为双引号
        taskData = taskData.replaceAll("'", "\"");


        // 显示收到的数据信息
        System.out.println(taskData);

        Task task = new Task();

        task.setDeviceId(pyData.getuuid());
        task.setDate(pyData.getDate());
        task.setUsername(pyData.getUsername());
        task.setInfo(taskData);
        // 入库
        this.taskService.insert(task);
    }

    private PyData getPyData(String data) throws JsonProcessingException {
        // 利用Jackson进行数据的解析
        // 将接收到的Json字符串映射为一个Java类实例（PyData）
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.readValue(data, PyData.class);
    }




    /**
     * 解析并处理数据 已弃用
     */
    public void process(String data) throws JsonProcessingException, JSONException {

        String replacedData = data.replaceAll("__", "\\\\\\\\");

        PyData pyData = getPyData(replacedData);

        if (TypeConst.BASE_INFO.equals(pyData.getTypeCode())) {
            //
            String taskData = pyData.getData();

            // 将单引号替换为双引号
            taskData = taskData.replaceAll("'", "\"");


            // 显示收到的数据信息
            System.out.println(taskData);

            Task task = new Task();

            task.setDeviceId(pyData.getuuid());
            task.setDate(pyData.getDate());
            task.setUsername(pyData.getUsername());
            task.setInfo(taskData);
            // 入库
            this.taskService.insert(task);
        } else if (TypeConst.NETWORK_INFO.equals(pyData.getTypeCode())) {
            String taskData = pyData.getData();

            // 将单引号替换为双引号
            taskData = taskData.replaceAll("'", "\"");

            // 显示收到的数据信息
            System.out.println(taskData);

            Task task = new Task();
            task.setDeviceId(pyData.getuuid());
            task.setDate(pyData.getDate());
            task.setUsername(pyData.getUsername());
            // task.setNetwork(taskData);
            // 入库
            this.taskService.update(task);
        } else if (TypeConst.INSTALLED_SOFT.equals(pyData.getTypeCode())) {
            String taskData = pyData.getData();

            // 将单引号替换为双引号
            taskData = taskData.replaceAll("'", "\"");

            // 显示收到的数据信息
            System.out.println(taskData);

            Task task = new Task();
            task.setDeviceId(pyData.getuuid());
            task.setDate(pyData.getDate());
            task.setUsername(pyData.getUsername());
            // task.setService(taskData);
            // 入库
            this.taskService.update(task);
        } else if (TypeConst.PATCH_INFO.equals(pyData.getTypeCode())) {
            String taskData = pyData.getData();

            // 将单引号替换为双引号
            taskData = taskData.replaceAll("'", "\"");

            // 显示收到的数据信息
            System.out.println(taskData);

            Task task = new Task();
            task.setDeviceId(pyData.getuuid());
            task.setDate(pyData.getDate());
            task.setUsername(pyData.getUsername());
            //  task.setPatch(taskData);
            // 入库
            this.taskService.update(task);
        } else if (TypeConst.PORT_INFO.equals(pyData.getTypeCode())) {
            String taskData = pyData.getData();

            // 将单引号替换为双引号
            taskData = taskData.replaceAll("'", "\"");

            // 显示收到的数据信息
            System.out.println(taskData);

            Task task = new Task();
            task.setDeviceId(pyData.getuuid());
            task.setDate(pyData.getDate());
            task.setUsername(pyData.getUsername());
//            task.setPort(taskData);
            // 入库
            this.taskService.update(task);
        } else if (TypeConst.SHADOW_ACCOUNT.equals(pyData.getTypeCode())) {
            String taskData = pyData.getData();

            // 将单引号替换为双引号
            taskData = taskData.replaceAll("'", "\"");

            // 显示收到的数据信息
            System.out.println(taskData);

            Task task = new Task();
            task.setDeviceId(pyData.getuuid());
            task.setDate(pyData.getDate());
            task.setUsername(pyData.getUsername());
//            task.setShadow(taskData);
            // 入库
            this.taskService.update(task);
        } else if (TypeConst.IMAGE_HIJACK.equals(pyData.getTypeCode())) {
            String taskData = pyData.getData();

            // 将单引号替换为双引号
            taskData = taskData.replaceAll("'", "\"");

            // 显示收到的数据信息
            System.out.println(taskData);

            Task task = new Task();
            task.setDeviceId(pyData.getuuid());
            task.setDate(pyData.getDate());
            task.setUsername(pyData.getUsername());
//            task.setHijack(taskData);
            // 入库
            this.taskService.update(task);
        } else if (TypeConst.BASELINE_INFO.equals(pyData.getTypeCode())) {
            String taskData = pyData.getData();

            // 将单引号替换为双引号
            taskData = taskData.replaceAll("'", "\"");

            // 显示收到的数据信息
            System.out.println(taskData);

            Task task = new Task();
            task.setDeviceId(pyData.getuuid());
            task.setDate(pyData.getDate());
            task.setUsername(pyData.getUsername());
//            task.setBenchmark(taskData);
            // 入库
            this.taskService.update(task);
        } else if (TypeConst.RUN_ONCE.equals(pyData.getTypeCode())) {
            String taskData = pyData.getData();

            // 将单引号替换为双引号
            taskData = taskData.replaceAll("'", "\"");

            // 显示收到的数据信息
            System.out.println(taskData);

            Task task = new Task();
            task.setDeviceId(pyData.getuuid());
            task.setDate(pyData.getDate());
            task.setUsername(pyData.getUsername());
//            task.setStartup(taskData);
            // 入库
            this.taskService.update(task);
        } else if (TypeConst.BASELINE_CHECK_RESULT_INFO.equals(pyData.getTypeCode())) {
            String taskData = pyData.getData();

            // 将单引号替换为双引号
            taskData = taskData.replaceAll("'", "\"");

            // 显示收到的数据信息
            System.out.println(taskData);

            Task task = new Task();
            task.setDeviceId(pyData.getuuid());
            task.setDate(pyData.getDate());
            task.setUsername(pyData.getUsername());
//            task.setResult(taskData);
            // 入库
            this.taskService.update(task);
        }


        System.out.println(data);
    }



    /**
     * 解析并处理数据 已弃用
     */
    public Object getEntityData(String data, Class clazz) throws JsonProcessingException {
        // 利用Jackson进行数据的解析
        //  返回指定实体类型的实例
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.readValue(data, clazz);
    }


}
